window.add = 1;

function addElement() {
    var ni = document.getElementById('myDiv');
    var numi = document.getElementById('theValue2');
    var num = (document.getElementById('theValue2').value -1)+ 2;
    numi.value = num;
    var newdiv = document.createElement('div');
    var divIdName = 'my'+num+'Div';
    newdiv.setAttribute('id',divIdName);
    add++;
    ni.appendChild(newdiv);
}

function tag(text1, text2) {
    if ((document.selection)) {
       document.message.article.focus();
       document.message.document.selection.createRange().text = text1+document.message.document.selection.createRange().text+text2;
    } else 
    if (document.forms['message'].elements['article'].selectionStart != undefined) {
        var element = document.forms['message'].elements['article'];
        var len = document.message.article.selectionStart;
        var str = element.value;
        var scroll =  document.message.article.scrollTop; 
        var start = element.selectionStart;
        var length = element.selectionEnd - element.selectionStart;
        element.value = str.substr(0, start) + text1 + str.substr(start, length) + text2 + str.substr(start + length);
        var scroll2 = scroll + text1.length + text2.length + length;
        document.message.article.scrollTop = scroll2;
        var len2 = text1.length + len + text2.length + length;
            document.message.article.setSelectionRange(len2,len2);
        document.message.article.focus();
        } else {
        document.message.article.value += text1+text2;
    }
}

function add_article_to_bookmarks(article_id, user_id, url)
{
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("rating").innerHTML = window.rating;
        }
    }
    xmlhttp.open("GET","/vote.php?user_id="+user_id+"&article_id="+article_id, true);
    xmlhttp.send();
    b = document.getElementById("bfav").src;
    if (b == url+"/inc/php/html/themes/0/images/star_0.png") {
        document.getElementById("bfav").src = "/inc/php/html/themes/0/images/star_1.png";
    } else {
        document.getElementById("bfav").src = "/inc/php/html/themes/0/images/star_0.png";
    }
}

window.onload = function() {
    if (window.trigger_0 == 1) {
        b = document.getElementById("bfav").src;
        if (b == "http://miha.ru/inc/php/html/themes/0/images/add_image.png") {
            document.getElementById("bfav").src = "http://miha.ru/inc/php/html/themes/0/images/star_0.png";
        } else {
            document.getElementById("bfav").src = "http://miha.ru/inc/php/html/themes/0/images/star_1.png";
        }
    }
}

function gallery_vote(work_id, total, count, mark)
{
    // total - mark that was
    // mark - mark of user
    
    if ((mark < 6) & (mark > 0)) {
        // If mark is OK
        if (window.XMLHttpRequest) {
            xmlhttp=new XMLHttpRequest();
        } else {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                //document.getElementById("rating").innerHTML = window.rating;
            }
        }
        xmlhttp.open("GET", "/gallery.php?action=vote&id="+work_id+"&id2="+mark, true);
        xmlhttp.send();

        document.getElementById("child").style.display="none"; 
        count = (count == 0) ? 1 : count;
        total = (total + mark)/count;
        var total2 = Math.floor(total);
        var style = "gallery_votebutton gallery_votebutton"+total2;
        document.getElementById("parent").innerHTML = "<div class=\""+style+"\" style=\"border-radius: 4px;\">"+total.toFixed(1)+"</div>";
    }
}

jQuery(document).ready(function() {
  var parents = jQuery('.vocabindex.tree .parent');
  parents.removeClass('expanded').addClass('collapsed').find('ul').hide();
  parents.children('a').click(function () {
    jQuery(this).parent().toggleClass('collapsed').toggleClass('expanded').find('ul').eq(0).slideToggle('fast');
    return false;
  }).focus(function () {
    jQuery(this).blur();
  });
  parents.children('a.inpath').click();
});

function removeElement(parentDiv, childDiv) {
     if (childDiv == parentDiv) {
          // alert("The parent div cannot be removed.");
     }
     else if (document.getElementById(childDiv)) {     
          var child = document.getElementById(childDiv);
          var parent = document.getElementById(parentDiv);
          parent.removeChild(child);
     }
     else {
          // alert("Child div has already been removed or does not exist.");
          return false;
     }
}


function getClientWidth()
{
    return document.compatMode=='CSS1Compat' && document.documentElement.clientWidth;
}

function getClientHeight()
{
    return document.compatMode=='CSS1Compat' && document.documentElement.clientHeight;
}
function setPos() {
    var w = getClientWidth();
    var h = getClientHeight();
    document.getElementById('popup').style.left = (parseInt(w/2)-212)+"px";
    document.getElementById('popup').style.top = (parseInt(h/2)-128)+"px";

}
setPos();

function $(v) {
    return(document.getElementById(v));
}
function agent(v) {
    return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0));
}
function xy(e,v) {
    return(v?(agent('msie')?event.clientY+document.body.scrollTop:e.pageY):(agent('msie')?event.clientX+document.body.scrollTop:e.pageX));
}

function dragOBJ(d,e) {
    document.getElementById('msg_textarea').focus();
    function drag(e) {
        if (!stop) {
            d.style.top=(tX=xy(e,1)+oY-eY+'px');
            d.style.left=(tY=xy(e)+oX-eX+'px');
        } 
    }
    var oX = parseInt(d.style.left), oY = parseInt(d.style.top), eX=xy(e), eY = xy(e,1), tX, tY, stop;
    document.onmousemove = drag;
    document.onmouseup = function()
    {
        stop=1;
        document.onmousemove=''; 
        document.onmouseup='';
    };
}

function delete_item(what, id)
{
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("rating").innerHTML = window.rating;
        }
    }
    xmlhttp.open("GET","/edit.php?what="+what+"&id="+id, true);
    xmlhttp.send();
    document.getElementById('list_item'+id).style.display = "none";
}

function settings_set(conf_name)
{
    // Именение отдельных настроек CMS
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("rating").innerHTML = window.rating;
        }
    }
    xmlhttp.open("GET","/settings.php?conf_name="+conf_name, true);
    if (conf_name == "show_header") {
        if (document.getElementById('check1').checked == false) {
            document.getElementById('header').style.display = "none";
        } else {
            document.getElementById('header').style.display = "block";
        }
    }
    xmlhttp.send();
}

function shAuthPassword(n)
{
    var el = document.getElementById("password") || false;
    if(!el) { 
        return;
    }
    if (el.getAttribute('type') == 'text') {
        el.setAttribute('type', 'password');
    } else {
        el.setAttribute('type', 'text');
    }
    if (n == 2) {
        var el2 = document.getElementById("password_repeat") || false;
        if(!el2) { 
            return;
        }
        if (el2.getAttribute('type') == 'text'){
            el2.setAttribute('type', 'password');
        }
        else {
            el2.setAttribute('type', 'text');
        }
    }
}

function switchTab(n)
{
    if ((n <= 0) || (n > 32)) {
        return;
    }
    document.getElementById('tab1').style.display = "none";
    document.getElementById('tab_1').className = "tab_item";
    
    document.getElementById('tab2').style.display = "none";
    document.getElementById('tab_2').className = "tab_item";
    
    document.getElementById('tab'+n).style.display = "block";
    document.getElementById('tab_'+n).className = "tab_active";
}

function deleteStatItem(n)
{
    if (n < 0) return;
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("rating").innerHTML = window.rating;
        }
    }
    xmlhttp.open("GET","/statistics.php?deleteItem="+n, true);
    document.getElementById('item'+n).style.display = "none";
    xmlhttp.send();
}
function deleteUser(n)
{
    if (n < 0) return;
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            //document.getElementById("rating").innerHTML = window.rating;
        }
    }
    xmlhttp.open("GET","/user.php?deleteUser="+n, true);
    document.getElementById('item_'+n).style.display = "none";
    xmlhttp.send();
}
